package com.eipc;

import org.androidpn.sdk.Constants;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.text.Html;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.eipc.model.ReplyResponse;
import com.eipc.util.DBHelper;
import com.eipc.util.HttpManager;
import com.eipc.util.JsonParse;
import com.eipc.util.Tools;
/**
 * 已发送
 * @author x_liaolijun
 *
 * 2010-9-4 上午11:44:56
 *  
 * ReceiveActivity
 *
 */
public class ReceivedActivity extends RefreshCommonActivity {
	private int type;   //类型(由于重要通告和短信和我要报告一样的)
	private ListView mListView;
	private Cursor mCursor;
	private String mServerId;
	private int mId;
	private Cursor minfo;
	private AlertDialog.Builder mBuilder;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mBuilder = new Builder(this).setTitle(R.string.tip);
		Intent i = getIntent();
		if(i==null){
			type = ImportNoticeActivity.TYPE;
		}
		else{
			type = i.getIntExtra("type", ImportNoticeActivity.TYPE);
		}
		setContentView(R.layout.import_notice_receive);
		initView();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		mCursor = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE_SEND,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_SENT_ISSUER,DBHelper.FIELD_PERSON_ID,new String[]{DBHelper.FIELD_IMPORT_NOTICE_APPTYPE},new String[]{String.valueOf(type)},DBHelper.FIELD_IMPORT_NOTICE_SENT_SEND_TIME+" desc"+","+DBHelper.FIELD_ID+" desc");
		mListView.setAdapter(new ContentAdapter(mCursor));
	}
	
	protected void initView(){
		mDB = new DBHelper(this);
		mListView = (ListView)findViewById(R.id.list1);
		mListView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				showDailog((Cursor)mListView.getItemAtPosition(position));
			}
		});
	}
	
	@Override
	protected void updateView() {
		super.updateView();
		mCursor = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE_SEND,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_SENT_ISSUER,DBHelper.FIELD_PERSON_ID,new String[]{DBHelper.FIELD_IMPORT_NOTICE_APPTYPE},new String[]{String.valueOf(type)},DBHelper.FIELD_IMPORT_NOTICE_SENT_SEND_TIME+" desc"+","+DBHelper.FIELD_ID+" desc");
		mListView.setAdapter(new ContentAdapter(mCursor));
	}
	
	/*
	 * 创建对话框
	 * 
	 * @param info
	 */
	private void showDailog(Cursor info){
		try{
			mServerId = info.getString(info.getColumnIndex(DBHelper.FIELD_IMPORT_NOTICE_SENT_SERVERID));
			mId = info.getInt(0);
			minfo = info;
			Log.e("------load reply mServerId-------",mServerId);
			Log.e("------load reply mId-------",String.valueOf(mId));
			if(mId>0&&mServerId!=null&&!"".equals(mServerId)){
				new ReplyTask().execute();
			}
		}catch(Exception e){
			Log.e("get serverID"," error : ",e);
		}
	}
	
	private void showDailogWidthReply(Cursor info){
		final Dialog dialog = new Dialog(this);
		dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		dialog.getWindow().setLayout(480, 650);
		View view = getLayoutInflater().inflate(R.layout.import_notice_dialog, null);
		ListView lv = (ListView)view.findViewById(R.id.list1);
		int id = info.getInt(0);
		Cursor c = null;
		TextView tv = (TextView) view.findViewById(R.id.content);
		tv.setText(info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_CONTENT)));
		tv = (TextView)view.findViewById(R.id.tv_title);
		switch(type){
		case DBHelper.APP_TYPE_IMPORT_NOTICE://重要通知
			tv.setText(getString(R.string.import_notice_dialog_title,getString(R.string.import_notice),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TYPE))));
			c = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE_SEND_CHILD,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_SENT_RECEIVER,DBHelper.FIELD_PERSON_ID, new String[]{DBHelper.FIELD_IMPORT_NOTICE_SENT_CHILD_ID}, new String[]{String.valueOf(id)});
			break;
		case DBHelper.APP_TYPE_ME_REPORT://我要找茬
			tv.setText(R.string.me_report);
			c = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE_SEND_CHILD, new String[]{DBHelper.FIELD_IMPORT_NOTICE_SENT_CHILD_ID}, new String[]{String.valueOf(id)});
			break;
		case DBHelper.APP_TYPE_WORK_REPORT://工作汇报
			tv.setText(getString(R.string.import_notice_dialog_title,getString(R.string.work_report),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TYPE))));
			c = mDB.queryAll(DBHelper.TB_IMPORT_NOTICE_SEND_CHILD,DBHelper.TB_PERSON,DBHelper.FIELD_IMPORT_NOTICE_SENT_RECEIVER,DBHelper.FIELD_PERSON_ID, new String[]{DBHelper.FIELD_IMPORT_NOTICE_SENT_CHILD_ID}, new String[]{String.valueOf(id)});
			break;
		}
		tv = (TextView)view.findViewById(R.id.issuer);
		tv.setText(Html.fromHtml(getString(R.string.control_signer,info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_ISSUE_TIME)))));
		tv = (TextView) view.findViewById(R.id.sender);
		tv.setText(Html.fromHtml(getString(R.string.control_publishor,info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)),info.getString(info.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_SEND_TIME)))));
		lv.setAdapter(new ItemAdapter(c));
		Button b = (Button)view.findViewById(R.id.btn_close);
		b.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				dialog.dismiss();
			}
		});
		dialog.setContentView(view);
		dialog.show();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if(mCursor!=null) mCursor.close();
	}
	
	class ContentAdapter extends CursorAdapter{
		public ContentAdapter(Cursor cursor){
			super(ReceivedActivity.this, cursor);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = getLayoutInflater().inflate(
					R.layout.control_center_item, null);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			ImageView iv = (ImageView)view.findViewById(R.id.iv_type);
			String type = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TYPE));
			iv.setImageResource(Tools.getIconByType(type));
			TextView tv = (TextView)view.findViewById(R.id.content);
			if(ReceivedActivity.this.type == DBHelper.APP_TYPE_ME_REPORT){
				tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_CONTENT)));
			}else{
				tv.setText("["+type+"]"+cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_CONTENT)));
			}
			tv = (TextView) view.findViewById(R.id.signer);
			tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)));
			tv = (TextView) view.findViewById(R.id.date);
			tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SEND_TIME)));
		}	
	}
	
	class ItemAdapter extends CursorAdapter{
		public ItemAdapter(Cursor cursor){
			super(ReceivedActivity.this, cursor);
		}
		
		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = getLayoutInflater().inflate(
					R.layout.received_item, null);
			return view;
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			TextView tv = (TextView) view.findViewById(R.id.tv_name);
			if(ReceivedActivity.this.type == DBHelper.APP_TYPE_ME_REPORT){
				tv.setText("系统");
			}else{
				tv.setText(cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_PERSON_NAME)));
			}
			tv = (TextView) view.findViewById(R.id.tv_status);
			tv.setText(mDB.getIsReadString(cursor.getInt(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_ISREAD))));
			tv = (TextView) view.findViewById(R.id.tv_resume_time);
			String content = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_REPLYCONTENT));
			String rTime = cursor.getString(cursor.getColumnIndexOrThrow(DBHelper.FIELD_IMPORT_NOTICE_SENT_REPLYTIME));
			tv.setText((content!=null?content:"")+" "+(rTime!=null?rTime:""));
		}	
	}
	
	class ReplyTask extends CommonTask<Void>{
		private String mResult;

		public ReplyTask(){
			super(ReceivedActivity.this,getString(R.string.loadReplying));
		}
		
		@Override
		protected Void doInBackground(Void... params) {
			SharedPreferences sdkPreferences = getSharedPreferences(Constants.SDK_PREFERENCES, Context.MODE_PRIVATE);
			String mobileID = sdkPreferences.getString(Constants.XMPP_USERNAME,"");
			try {
				mResult = HttpManager.loadReply(ReceivedActivity.this.getResources().getString(R.string.url_load_reply),mobileID,mServerId,String.valueOf(mId));
				Log.e("------load reply mResult-------",mResult);
			} catch (Exception e) {
				Log.e("Reply exception : ",e.getMessage());
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			if(mResult!=null && !"".equals(mResult)){
				ReplyResponse resp = JsonParse.parseReplyRespnse(mResult);
				Log.e("------load reply resp.id-------",String.valueOf(resp));
				if(resp!=null&&resp.success&&resp.id!=null&&resp.rs!=null&&resp.rs.length>0){//回复成功
					Log.e("------load reply resp.rs.length-------",String.valueOf(resp.rs.length));
					mDB.updateImportNoticeReplys(resp.rs);
				}else{
					//回复失败
				}
			}else{
				//出错
			}
			showDailogWidthReply(minfo);
		}
	}
}
